<?php
namespace app\examScore\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;
//--优异奖
class ExamYouyiPrize extends Base {
	
	public function index() {
		$fields = "班级,学号,姓名,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,化学,化学名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,班排,级排";
		$list = explode(",", $fields);
		$this->assign('list',$list);
		return $this->fetch();
	}

	public function pageQuery() {
		$examId = input("examId");
		if(!$examId) return [];
		$grade = input("grade");
		$clazz = input("clazz");
		$gradeId = intval(input("gradeId"));
		$page = input("page");
		$rows = input("rows");
		$sort = input("sort");
		$order = input("order");
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
		}
		if($clazz) $clazz = $gradeId*100 + intval($clazz);
		$result = Db::table("成绩".$grade)->alias("a")
				  ->join("优异奖设置 b", "a.实验班 = b.班级层次", "INNER")
				  ->field("班级,学号,姓名,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,化学,化学名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,班排,级排")
				  ->where("考试ID=".$examId." and 级排<=b.排名".($clazz ? " and 班级=".$clazz:""))
				  ->order($sort ? $sort." ".$order : "班级,学号")
				  ->page($page, $rows)
				  ->select();
		$count = Db::table("成绩".$grade)->alias("a")
				  ->join("优异奖设置 b", "a.实验班 = b.班级层次", "INNER")
				  ->where("考试ID=".$examId." and 级排<=b.排名".($clazz ? " and 班级=".$clazz:""))
				  ->count();
		$grid["rows"] = selectResult($result);
		$grid["total"] = $count;
		return $grid;
	}

	public function exportExcel() {
		$examId = input("examId");
		if(!$examId) return [];
		$gradeId = intval(input("gradeId"));
		$grade = input("grade");
		$clazz = input("clazz");
		$sort = input("sort");
		$order = input("order");
		if($clazz) $clazz = $gradeId*100 + intval($clazz);
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
		}
		$result = Db::table("成绩".$grade)->alias("a")
				  ->join("优异奖设置 b", "a.实验班 = b.班级层次", "INNER")
				  ->field("班级,学号,姓名,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,化学,化学名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,班排,级排")
				  ->where("考试ID=".$examId." and 级排<=b.排名".($clazz ? " and 班级=".$clazz:""))
				  ->select();
		$result = selectResult($result);
		//excel表头内容
		$check = "";
		if($gradeId == 1) $check = "";
		else if($gradeId == 2) $check = "物理,物理名次,";
		else $check = "物理,物理名次,化学,化学名次,";
		$fields = "班级,学号,姓名,语文,语文名次,数学,数学名次,英语,英语名次,".$check."政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,班排,级排";
		$list = explode(",", $fields);
		$header = [];
		foreach ($list as $key => $value){
			$header[$value] = $value;
		}
		Excel::toExcel($result,'优异奖',$header);
		exit;
	}

}
